﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GT.UI.WinForms
{
    public partial class ProgressDialog : Form
    {
        public event EventHandler Completed;
        public bool AllowDrag { get; set; }
        public bool OpenModal { get; set; }

        public ProgressDialog()
        {
            InitializeComponent();
            AllowDrag = true;
        }

        public string Title
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        public new string Text
        {
            get
            {
                return Status.Text;
            }
            set
            {
                Status.Text = value;
            }
        }

        public ProgressBarStyle Style
        {
            get { return Progress.Style; }
            set { Progress.Style = value; }
        }

        public int Value
        {
            get { return Progress.Value; }
            set
            {
                Progress.Value = value;
                if (Progress.Value >= MaxValue)
                {
                    Completed.Raise(this);
                }
            }
        }

        public int MinValue
        {
            get { return Progress.Minimum; }
            set { Progress.Minimum = value; }
        }

        public int MaxValue
        {
            get { return Progress.Maximum; }
            set { Progress.Maximum = value; }
        }

        public ProgressDialog Open()
        {
            if (OpenModal)
                this.ShowDialog();
            else
                this.Show();
            return this;
        }

        public int SetValue(int value = 0)
        {
            Value = value;
            return Value;
        }

        public int SetValue(int completed = 0, int total = 100)
        {
            if (MinValue != 0) MinValue = 0;
            if (MaxValue != 100) MaxValue = 100;
            Value = (int)Math.Round((double)(completed * 100) / total, 0);
            return Value;
        }

        protected override void WndProc(ref Message message) 
        { 
            const int WM_SYSCOMMAND = 0x0112; 
            const int SC_MOVE = 0xF010; 
 
            switch (message.Msg) 
            { 
                case WM_SYSCOMMAND: 
                   int command = message.WParam.ToInt32() & 0xfff0;
                   if (command == SC_MOVE && !AllowDrag)
                   {
                       return;
                   }
                   break; 
            } 
 
            base.WndProc(ref message); 
        } 

    }
}
